package com.magina.nety.http;

import cn.hutool.core.util.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Map;
import java.util.Random;
import java.util.UUID;

@RestController
@RequestMapping("/rest")
public class RestHttpController {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 方法描述: [multipart/form-data rest http request
     *          方式一: 下面代码
     *          方法二:  http://ip:8080/test?code='asd'&name='test' 此种传参亦可以]</br>
     * 初始作者: Magina<br/>
     * 创建日期:17:36 2019/8/12<br/>
     * 开始版本: 1.0.0<br/>
     * =================================================<br/>
     * 修改记录：<br/>
     * 修改作者 日期 修改内容<br/>
     * ================================================<br/>
     * @param
     * @return
     */
    @GetMapping(value = "/testRest")
    public Object testRest(){
        String url = "http://localhost:8095/restserve/postArg";
        MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
        String callId = UUID.randomUUID().toString();
        map.add("code",callId);
        map.add("num",RandomUtil.randomNumber());
        HttpHeaders headers = new HttpHeaders();
//        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<MultiValueMap<String, Object>>(map, headers);
        Map resutlMap =  restTemplate.postForObject(url, request,Map.class);
        return resutlMap;

    }
}
